package com.shopp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shopp.auto.JSONAuthentication;
import com.shopp.entity.ShOrder;
import com.shopp.entity.UserZiliao;
import com.shopp.mapper.ShFoodMapper;
import com.shopp.mapper.ShOrderMapper;
import com.shopp.mapper.ShUserMapper;
import com.shopp.mapper.UserZiliaoMapper;
import com.shopp.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shopp
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/sh-order")
public class ShOrderController extends JSONAuthentication {
    @Autowired
    ShOrderMapper shOrderMapper;
    @Autowired
    ShUserMapper shUserMapper;
    @Autowired
    ShFoodMapper shFoodMapper;
    @ApiOperation(value = "添加订单")
    @PostMapping("/add")
    public void addOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("price") float price,@RequestParam("foId") int foId,@RequestParam("num") int num) throws Exception {
        ShOrder shOrder=new ShOrder();
        shOrder.setUserId(userId);
        shOrder.setPrice(price);
        Date date=new Date();
        Timestamp timestamp=new Timestamp(date.getTime());
        shOrder.setCreatTime(timestamp);
        shOrder.setFoId(foId);
        shOrder.setNum(num);
        if (shOrderMapper.insert(shOrder)> 0) {
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delZiLiao(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = shOrderMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查看所有订单")
    @PostMapping("/LookAllOrder")
    public void LookAllOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int  size, @RequestParam("current") int current) throws Exception {
        Page<ShOrder> page=new Page<>(current,size);
        Page<ShOrder> pages=shOrderMapper.selectPage(page,null);
        List<ShOrder> records = pages.getRecords();
        int total=shOrderMapper.selectCount(null);
        for(ShOrder a:records){
        a.setShUser(shUserMapper.selectById(a.getUserId()));
        a.setShFood(shFoodMapper.selectById(a.getFoId()));
        }
        if (records.size() > 0) {
            Result ok = Result.ok().data("order",records).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无商品");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据用户id查询订单")
    @PostMapping("/LookUserIdFindOrder")
    public void LookUserIdFindOrder(HttpServletRequest request, HttpServletResponse response, @RequestParam("userId") int  userId, @RequestParam("size") int  size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.like("user_id",userId);
        Page<ShOrder> page=new Page<>(current,size);
        Page<ShOrder> pages=shOrderMapper.selectPage(page,wrapper);
       List<ShOrder> list= pages.getRecords();
       for (ShOrder a:list){
           a.setShUser(shUserMapper.selectById(a.getUserId()));
           a.setShFood(shFoodMapper.selectById(a.getFoId()));
       }
       int total=shOrderMapper.selectCount(wrapper);
        if (list.size()>0) {
            Result ok = Result.ok().data("order",list).data("total",total);
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无资料，快去添加吧");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "根据订单id修改订单状态(确认收货)")
    @PostMapping("/OrderUpdStatusByID")
    public void OrderUpdStatusByID(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id) throws Exception {
       ShOrder shOrder= shOrderMapper.selectById(id);
       shOrder.setStatus(1);
        if (shOrderMapper.updateById(shOrder)>0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error().message("暂无资料，快去添加吧");
            this.WriteJSON(request, response, error);
        }
    }

}

